package com.zhkj.txg.module.order.adapter

import android.view.View
import android.widget.EditText
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseNodeAdapter
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.txg.R
import com.zhkj.txg.module.find.adapter.FindPictureAdapter
import com.zhkj.txg.module.find.entity.FindPictureEntity
import com.zhkj.txg.module.order.vm.EvaluateUICommentEntity
import com.zhkj.txg.module.order.vm.EvaluateUIGoodsEntity
import com.zhkj.txg.utils.ImageUtils
import com.zhkj.txg.utils.TextWatcherWrapper

class OrderEvaluateAdapter(
    val onAddPicture: (uiPosition: Int, realCount: Int) -> Unit,
    val onDeletePicture: (UiPosition: Int, deletePosition: Int) -> Unit
) : BaseNodeAdapter() {

    companion object {
        val TYPE_PRODUCT = 0
        val TYPE_COMMENT = 1
    }

    init {
        addNodeProvider(EvaluateProductProvider())
        addNodeProvider(EvaluateCommentProvider())
    }

    override fun getItemType(data: List<BaseNode>, position: Int): Int = when (data[position]) {
        is EvaluateUIGoodsEntity -> TYPE_PRODUCT
        else -> TYPE_COMMENT
    }

    override fun onItemViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        if (viewType == TYPE_COMMENT) {
            // 设置图片RecyclerView
            val rv = viewHolder.getView<RecyclerView>(R.id.rvEvaluate)
            rv.layoutManager = GridLayoutManager(context, 3)
            rv.adapter = FindPictureAdapter(6).apply {
                setOnItemClickListener { _, _, position ->
                    // 设置添加按钮点击事件
                    if (data[position].itemType == FindPictureEntity.TYPE_ADD)
                        onAddPicture(viewHolder.layoutPosition, getRealPictureCount())
                }
                setOnItemChildClickListener { _, view, position ->
                    // 设置删除按钮点击事件
                    if (view.id == R.id.ivPicDelete)
                        onDeletePicture(viewHolder.layoutPosition, position)
                }
            }
            // 设置商品评论数据
            val et = viewHolder.getView<EditText>(R.id.etEvaluate)
            et.addTextChangedListener(TextWatcherWrapper {
                (getItem(viewHolder.layoutPosition) as? EvaluateUICommentEntity)?.content =
                    it.toString()
            })
        }
    }
}

class EvaluateProductProvider : BaseNodeProvider() {
    override val itemViewType: Int = OrderEvaluateAdapter.TYPE_PRODUCT
    override val layoutId: Int = R.layout.item_evaluate_product

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        (data as? EvaluateUIGoodsEntity)?.let {
            val imageView = helper.setText(R.id.tvEvaluateGoodsName, it.goodsName)
                .setText(R.id.tvEvaluateSpecValue, it.specValue)
                .getView<ImageView>(R.id.ivEvaluateProduct)
            ImageUtils.loadRoundCornerImage(context, imageView, it.image)
        }
    }
}

class EvaluateCommentProvider : BaseNodeProvider() {
    override val itemViewType: Int = OrderEvaluateAdapter.TYPE_COMMENT
    override val layoutId: Int = R.layout.item_evaluate_comment

    init {
        addChildClickViewIds(R.id.viewEvaluateGood, R.id.viewEvaluateMiddle, R.id.viewEvaluateBad)
    }

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        (data as? EvaluateUICommentEntity)?.let {
            val editText = helper.getView<EditText>(R.id.etEvaluate)
            editText.setText(it.content)
            editText.setSelection(it.content.length)
            helper.getView<View>(R.id.viewEvaluateGood).isSelected = it.goodsRank == 1
            helper.getView<View>(R.id.viewEvaluateMiddle).isSelected = it.goodsRank == 2
            helper.getView<View>(R.id.viewEvaluateBad).isSelected = it.goodsRank == 3
            (helper.getView<RecyclerView>(R.id.rvEvaluate).adapter as? FindPictureAdapter)?.replaceData(
                it.imageList
            )
        }
    }

    override fun onChildClick(helper: BaseViewHolder, view: View, data: BaseNode, position: Int) {
        (data as? EvaluateUICommentEntity)?.let {
            when (view.id) {
                R.id.viewEvaluateGood -> it.goodsRank = 1
                R.id.viewEvaluateMiddle -> it.goodsRank = 2
                R.id.viewEvaluateBad -> it.goodsRank = 3
            }
            if (position > -1) getAdapter()?.notifyItemChanged(position)
        }
    }
}